﻿//************************************************************************************************
// Copyright © 2012 Steven M. Cohn. All Rights Reserved.
//
//************************************************************************************************

namespace Cookie.Baker
{
	using System;
	using System.Collections.Generic;
	using System.IO;
	using Microsoft.VisualStudio.TestTools.UnitTesting;
	using CookieChipper;


	[TestClass]
	public class CookieFileProviderTests
	{
		[TestMethod]
		public void TestFileProvider ()
		{
			string tempDir = Path.Combine(
				Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
				"Temp");

			Cookie cookie = new Cookie(new HashRef(), "ABCDEFGH");
			cookie.Path = Path.Combine(tempDir, cookie.Name + ".txt");
			cookie.Domain = "cookiechipper.codeplex.com";
			cookie.Accessed = DateTime.Now;
			cookie.Updated = DateTime.Now;

			cookie.Add(
				new Chip("chipOne")
				{
					Value = "111",
					Domain = "cookiechipper.codeplex.com",
					Flags = ChipFlags.Primary | ChipFlags.Client,
					Created = DateTime.Now,
					Expires = DateTime.Now
				});

			cookie.Add(
				new Chip("chipTwo")
				{
					Value = "222",
					Domain = "cookiechipper.codeplex.com",
					Flags = ChipFlags.Server,
					Created = DateTime.Now,
					Expires = DateTime.Now
				});

			bool result = CookieFileProvider.Save(cookie);
			Assert.IsTrue(result);

			cookie.Values.Clear();

			using (var provider = new CookieFileProvider(cookie.Path))
			{
				result = provider.LoadChips(cookie);
				Assert.IsTrue(result);
				Assert.AreEqual(2, cookie.Values.Count);
			}

			result = CookieFileProvider.Delete(cookie.Path);
			Assert.IsTrue(result);

			Assert.IsFalse(File.Exists(cookie.Path));
		}


		[TestMethod]
		public void DumpTextFiles ()
		{
			string FileNamePattern = "*.txt";

			string root = Path.Combine(
				Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
				@"Temp\Cookies");

			IEnumerable<string> files = Directory.EnumerateFiles(
				root, FileNamePattern, SearchOption.TopDirectoryOnly);

			var paths = files.GetEnumerator();
			while (paths.MoveNext())
			{
				Console.WriteLine(paths.Current);
			}
		}
	}
}
